package com.ruoyi.odorservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.Result;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.odorservice.entity.Company;
import com.ruoyi.odorservice.entity.CompanyIndustry;
import com.ruoyi.odorservice.mapper.CompanyIndustryMapper;
import com.ruoyi.odorservice.mapper.CompanyMapper;
import com.ruoyi.odorservice.model.bean.CompanyIndustryInfoBean;
import com.ruoyi.odorservice.model.bean.CompanyInfoListBean;
import com.ruoyi.odorservice.model.vo.CompanyListVo;
import com.ruoyi.odorservice.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {

    @Autowired
    private CompanyIndustryMapper companyIndustryMapper;

    @Override
    @Transactional
    public Result<Boolean> addCompany(CompanyIndustryInfoBean company) {
        if(hasSameCompany(company.getCompanyName(), null)) return Result.fail("企业名称已存在");
        Company companyInsert = new Company();
        //获取行业id集合 1,2,3
        String industryIds = company.getIndustryIds();
        String[] ids = new String[0];
        if(industryIds != null) ids = industryIds.split(",");
        List<CompanyIndustry> companyIndustryList = new ArrayList<>();
        BeanUtils.copyProperties(company, companyInsert);
        //插入企业
        this.baseMapper.insert(companyInsert);
        //获取要插入行业
        for (String id : ids) {
            companyIndustryList.add(new CompanyIndustry(companyInsert.getId(), Integer.parseInt(id)));
        }
        //行业不为空 插入行业 ,防止为空时sql 报错
        if(industryIds != null) companyIndustryMapper.insertList(companyIndustryList);
        return Result.ok(true);
    }

    @Override
    @Transactional
    public Result<Boolean> updateCompany(CompanyIndustryInfoBean company) {
        if(hasSameCompany(company.getCompanyName(), company.getId())) return Result.fail("企业名称已存在");
        Company companyUpdate = new Company();
        String industryIds = company.getIndustryIds();
        String[] ids = new String[0];
        if(industryIds != null) ids = industryIds.split(",");
        //请求传过来的 行业集合
        Set<CompanyIndustry> newSet = new HashSet<>();
        BeanUtils.copyProperties(company, companyUpdate);
        //更改企业信息
        this.baseMapper.updateById(companyUpdate);
        LambdaQueryWrapper<CompanyIndustry> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CompanyIndustry::getCompanyId, company.getId());
        //从数据库中查询出 企业所属行业
        List<CompanyIndustry> old = companyIndustryMapper.selectList(queryWrapper);
        Set<CompanyIndustry> oldSet = new HashSet<>(old);
        for (String id : ids) {
            newSet.add(new CompanyIndustry(companyUpdate.getId(), Integer.parseInt(id)));
        }
        //需要新增的集合
        Set<CompanyIndustry> insertSet = new HashSet<>(newSet);
        insertSet.removeAll(oldSet);
        //需要删除的集合
        Set<CompanyIndustry> deleteSet = new HashSet<>(oldSet);
        deleteSet.removeAll(newSet);
        List<CompanyIndustry> companyIndustryList = new ArrayList<>(insertSet);
        if(!insertSet.isEmpty() || !deleteSet.isEmpty()){
            if(!companyIndustryList.isEmpty()) companyIndustryMapper.insertList(companyIndustryList);
            if(!deleteSet.isEmpty()) companyIndustryMapper.deleteList(deleteSet);
        }
        return Result.ok(true);
    }

    @Override
    public Result<Page<CompanyListVo>> companyList(CompanyInfoListBean companyInfoListBean) {
        Integer page = companyInfoListBean.getPage();
        Integer limit = companyInfoListBean.getLimit();
        String companyName = companyInfoListBean.getCompanyName();
        List<CompanyListVo> companyList = this.baseMapper.selectCompanyPage((page - 1) * limit, limit, companyName);
        Integer total = this.baseMapper.selectCompanyPageCount(companyName);
        Page<CompanyListVo> companyListVoPage = new Page<>();
        companyListVoPage.setTotal(total);
        companyListVoPage.setRecords(companyList);
        return Result.ok(companyListVoPage);
    }


    //查询是否含有相同名称的公司
    private Boolean hasSameCompany(String companyName, Integer companyId) {
        Company company = this.baseMapper.selectOne(new LambdaQueryWrapper<Company>().eq(Company::getCompanyName, companyName));
        return company != null && !company.getId().equals(companyId);
    }
    
}
